package org.southyin.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.southyin.commons.CommonsResp;
import org.southyin.model.CmsCustomer;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(value = {"/article/save"})
public class FilterC_UserLogin implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        CmsCustomer loginer = (CmsCustomer) req.getSession().getAttribute("loginer");
        if (loginer == null) {
            resp.getWriter().println(new ObjectMapper().writeValueAsString(CommonsResp.failed(401, "用户未登录")));
        }else{
            chain.doFilter(req,resp);
        }
    }

    public void destroy() {

    }
}
